<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class CustomerAddress extends Model
{
    protected $table = 'customer_address';
    protected $fillable = ['customer_id', 'areas', 'address', 'contacts', 'phone', 'express_id', 'remark'];

    protected static function boot()
    {
        static::addGlobalScope('orderBy', function (Builder $query){
            $query->orderByDesc('id');
        });
        parent::boot();
    }

    /**
     * @param array $value
     * @author luffyzhao@vip.126.com
     */
    protected function setAreasAttribute(array $value)
    {
        $this->attributes['areas'] = implode('|', $value);
    }

    /**
     * @return array
     * @author luffyzhao@vip.126.com
     */
    protected function getAreasAttribute()
    {
        return explode('|', $this->attributes['areas']);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author luffyzhao@vip.126.com
     */
    public function customer()
    {
        return $this->belongsTo(Customer::class, 'customer_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author luffyzhao@vip.126.com
     */
    public function express()
    {
        return $this->belongsTo(Express::class, 'express_id');
    }
}
